<template>
  <div class="uni-page login">
    <div class="content theme--light">
      <div class="login-content">
        <div class="close-content" @click="goToBox">
          <span class="icon iconfont close">&#xe748;</span>
        </div>
        <img
          class="login-logo"
          src="/static/img/unboxingDetail/logo_light.png"
          draggable="false"
        />
        <div class="login-left">
          <van-tabs
            :background="main_bc_color"
            :color="'#00dcab'"
            :title-inactive-color="'#4d6168'"
            :title-active-color="'#4d6168'"
            v-model="activeTab"
          >
            <van-tab title="Register">
              <div>
                <div class="login-row">
                  <!-- <div class="login-row-label">E-mail</div> -->
                  <div class="login-row-label">E-mail</div>
                  <div class="login-row-input">
                    <div class="row-input">
                      <van-field
                        style="height: 100%"
                        v-model="registerForm.name"
                      />
                    </div>
                  </div>
                </div>
                <!-- <div class="login-row">
                  <div class="login-row-label">email verity code</div>
                  <div class="login-row-input">
                    <div class="row-input">
                      <van-field
                        style="height: 100%"
                        v-model="registerForm.verityCode"
                      />
                    </div>
                    <div class="send-btn">
                      <span>Send</span>
                    </div>
                  </div>
                </div> -->
                <div class="login-row">
                  <div class="login-row-label">Password</div>
                  <div class="login-row-input">
                    <div class="row-input">
                      <van-field
                        style="height: 100%"
                        v-model="registerForm.password"
                      />
                    </div>
                  </div>
                </div>
                <div class="login-row">
                  <div class="login-row-label" style="color: rgb(77, 193, 71)">
                    promotion
                  </div>
                  <div class="login-row-input">
                    <div class="row-input">
                      <van-field
                        style="height: 100%"
                        v-model="registerForm.promotionCode"
                        placeholder="Optional"
                      />
                    </div>
                  </div>
                </div>

                <div class="login-btn" @click="register">START UNBOXING</div>
                <div class="sign-tip">Or register with</div>
                <div class="other-row">
                  <button
                    id="google-signin-btn-52"
                    class="google-login-content"
                  >
                    <div class="google-login-btn">
                      <img
                        src="/static/img/main/google2.svg"
                        alt=""
                        class="google-icon"
                      />
                    </div>
                  </button>
                  <div class="google-login-btn">
                    <img
                      src="/static/img/main/facebook2.svg"
                      alt=""
                      class="fb-icon"
                    />
                  </div>
                </div>
              </div>
            </van-tab>
            <van-tab title="Login"
              ><div>
                <div class="login-row">
                  <div class="login-row-label">E-mail</div>
                  <div class="login-row-input">
                    <div class="row-input">
                      <van-field
                        style="height: 100%"
                        v-model="registerForm.name"
                      />
                    </div>
                  </div>
                </div>

                <div class="login-row">
                  <div class="login-row-label">Password</div>
                  <div class="login-row-input">
                    <div class="row-input">
                      <van-field
                        style="height: 100%"
                        v-model="registerForm.password"
                      />
                    </div>
                  </div>
                </div>

                <div class="login-btn" @click="toLogin">SINGN IN</div>
                <div class="sign-tip">Or register with</div>
                <div class="other-row">
                  <button
                    id="google-signin-btn-52"
                    class="google-login-content"
                  >
                    <div class="google-login-btn">
                      <img
                        src="/static/img/main/google2.svg"
                        alt=""
                        class="google-icon"
                      />
                    </div>
                  </button>
                  <div class="google-login-btn">
                    <img
                      src="/static/img/main/facebook2.svg"
                      alt=""
                      class="fb-icon"
                    />
                  </div>
                </div>
                <div class="forget-row" @click="goToForgetPsd">
                  FORGOT PASSWORD?
                </div>
              </div></van-tab
            >
            <van-tab title=""></van-tab>
            <van-tab title=""></van-tab>
          </van-tabs>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
var sha256 = require("js-sha256");
export default {
  data() {
    return {
      timeInterVer: 0,
      registerForm: {
        name: "",
        verityCode: "",
        password: "",
        promotionCode: "",
      },
      activeTab: 0,
    };
  },
  mounted() {
    if (this.$route.query.type == "login") {
      this.activeTab = 1;
    }
  },
  computed: {
    ...mapState([
      "userInfo",
      "bgColor",
      "reviewSearchBgColor",
      "color",
      "nav_bc",
      "main_bc_opacity",
      "main_bc_0_8",
      "main_color_0_1",
      "main_color_0_1_1",
      "main_bc_invert",
      "content_bc",
      "sub_color",
      "text_color",
      "dm_bc",
      "price_border",
      "main_bc_color",
      "item_bc",
      "menu_color",
      "unboxing_im_bc",
      "menu_d_bg",
      "menu_border",
      "pop_bg",
      "main_bc_color",
      "tab_bc",
    ]),
  },
  methods: {
    ...mapMutations(["setUserInfo", "setIsLogin"]),
    goToForgetPsd() {
      this.$router.push({
        name: "ForgetPsd",
      });
    },
    register() {
      this._showLoading();
      this.$http
        .post(`api/v1/user`, {
          name: this.registerForm.name,
          passWord: this.registerForm.password,
          promotionCode: this.promotionCode,
        })
        .then(async (res) => {
          this._dismissLoading();
          if (res.data && res.data.isSuccess) {
            this._showToast(res.data.message);
            this.activeTab = 1;
          } else {
            this._showToast(res.data.message);
          }
        })
        .catch((err) => {
          this._dismissLoading();
          this._showToast(err.message);
        });
    },
    toLogin() {
      clearInterval(this.timeInterVer);
      this._showLoading();
      this.$http
        .post(`api/v1/user/login2`, {
          userName: this.registerForm.name,
          passWord: sha256(this.registerForm.password),
        })
        .then(async (res) => {
          this._dismissLoading();
          if (res.data && res.data.isSuccess) {
            this.setUserInfo(res.data.resultData);
            this.setIsLogin(1);
            this.goToBox();

            this.timeInterVer = setInterval(() => {this.refreshToken()}, 9*60*1000);
          } else {
            this._showToast(res.data.message);
          }
        })
        .catch((err) => {
          this._dismissLoading();
          this._showToast(err.message);
        });
    },
    refreshToken() {
      this.$http
        .put(`api/v1/token`, {
          token: this.userInfo.token,
          refreshToken: this.userInfo.refreshToken,
        })
        .then(async (res) => {
          this._dismissLoading();
          if (res.data && res.data.isSuccess) {
            this.setUserInfo(res.data.resultData);
            this.setIsLogin(1);
          } else {
            this._showToast(res.data.message);
          }
        })
        .catch((err) => {
          this._dismissLoading();
          this._showToast(err.message);
        });
    },
    goToBox() {
      this.$router.push({
        name: "box",
      });
    },
    login() {
      this.setIsLogin(1);
    },
  },
};
</script>

<style lang="scss" scoped>
.uni-page {
  display: block;
  width: 100%;
  height: 100%;
  .content {
    box-sizing: border-box;
    .login-content {
      max-width: 850px;
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      position: relative;
      .close-content {
        position: absolute;
        top: calc(0px + 5px);
        right: 10px;
        z-index: 1;
        cursor: pointer;
        .close {
          font-size: 24px;
        }
      }

      .login-logo {
        width: 93px;
        height: 93px;
        margin-top: 52px;
      }

      .login-left {
        width: 100%;
        padding: 20px;
        box-sizing: border-box;
        .forget-row {
          text-align: center;
          cursor: pointer;
          color: #779099;
          font-size: 12px;
          font-weight: 500;
          line-height: normal;
          letter-spacing: 0.11px;
          margin-top: 10px;
        }
        .google-login-btn {
          width: 120px;
          height: 40px;
          border-radius: 40px;
          display: flex;
          justify-content: center;
          align-items: center;
          cursor: pointer;
          border: 1px solid var(--content-bc);
          background-color: var(--content-bc);
          margin: 0 10px;
        }
        .google-login-content {
          background: transparent;
          outline: none;
          border: none;
          padding: 0;
        }
        .other-row {
          display: flex;
          justify-content: center;
          align-items: center;
          margin: 12px 0;
        }
        .sign-tip {
          margin-top: 30px;
          font-size: 12px;
          text-align: center;
          color: #4d6168;
        }
        .login-btn {
          width: 100%;
          height: 45px;
          border-radius: 45px;
          background: linear-gradient(98deg, #f48836, #f4364c 100%);
          margin-top: 40px;
          display: flex;
          justify-content: center;
          align-items: center;
          cursor: pointer;
          color: #fff;
          font-weight: 700;
        }
        .login-head {
          width: 100%;
          height: 40px;
          display: flex;
          justify-content: flex-start;
          align-items: center;
          margin-bottom: 16px;
          color: #4d6168;
          border-bottom: 1px solid;
        }
        .login-row {
          width: 100%;
          position: relative;
          margin-bottom: 10px;
          .send-btn {
            position: absolute;
            top: 50%;
            right: 10px;
            -webkit-transform: translateY(-50%);
            transform: translateY(-50%);
            width: 114px;
            border-radius: 6px;
            height: 31px;
            font-size: 12px;
            display: flex;
            justify-content: center;
            align-items: center;
            background: linear-gradient(98deg, #f48836, #f4364c 100%);
            color: #fff;
          }
          .row-input {
            width: 100%;
            height: 100%;
            border: none;
            outline: none;
            padding: 0 20px;
            box-sizing: border-box;
            color: #82969d;
          }
          .login-row-label {
            font-size: 12px;
            margin-bottom: 8px;
            color: #4d6168;
          }
          .login-row-input {
            width: 100%;
            border-radius: 4px;
            position: relative;
            color: #82969d;
            height: 45px;
            border: 1px solid #82969d;
            background: var(--content-bc);
          }
        }
      }
    }
  }
}
</style>
<style lang="scss">
.login {
  .van-tab__text--ellipsis {
    font-size: 16px;
  }
  .van-cell {
    background: var(--content-bc);
  }
  .van-field__control {
    color: var(--text-color);
  }

  .login-row-input {
    background: var(--content-bc);
  }
  .van-tabs--line .van-tabs__wrap {
    border-bottom: 1px #4d6168 solid;
    margin-bottom: 16px;
  }
}
</style>